package com.mrobuy.jdvc.dao.impl;

import com.mrobuy.jdvc.dao.SyncStatDao;
import com.mrobuy.jdvc.entity.SyncState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository("syncStatDao")
public class SyncStatDaoImpl extends CommonDaoImpl implements SyncStatDao {

    private static Logger LOG = LoggerFactory.getLogger(SyncStatDao.class);


    @Override
    public List<SyncState> list() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from SYNC_STATE ");
        try {
            List<SyncState> list = getJdbcTemplate().query(sql.toString(),  new BeanPropertyRowMapper(SyncState.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public SyncState load(String shopName, String name) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from SYNC_STATE where SHOPNAME = ? and  NAME = ? ");
        try {
            SyncState record = (SyncState)getJdbcTemplate().queryForObject(sql.toString(), new Object[]{shopName, name},  new BeanPropertyRowMapper(SyncState.class));
            return record;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        return null;
    }
}
